package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.app.common.Result;
import com.app.model.*;
import com.app.service.InviteConfigLogService;
import com.app.service.InviteConfigService;
import com.app.utils.TokenUtils;
import com.app.vo.InviteConfigLogVo;
import com.github.yulichang.query.MPJQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/inviteConfig")
public class InviteConfigController {
    @Autowired
    private InviteConfigService inviteConfigService;

    @Autowired
    private InviteConfigLogService inviteConfigLogService;


    /**
     * 邀请规则变更日志列表
     * @param request
     * @param inviteConfigId
     * @return
     */
    @GetMapping("/admin/getInsertBasicConfigList")
    public Result getInsertBasicConfigList(Integer page, Integer limit,HttpServletRequest request,String inviteConfigId) {
        InviteConfigLogVo list = inviteConfigLogService.getInsertBasicConfigList(page,limit,inviteConfigId);
        return Result.success(list);
    }


    /**
     * 插入基本配置
     *
     * @param configModel 配置模型
     * @return {@link Result}
     */
    @PostMapping("/admin/insertBasicConfig")
    public Result insertBasicConfig(HttpServletRequest request,@RequestBody InviteConfigModel configModel) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (configModel.getId() != null && configModel.getId() > 0) {
            configModel.setUpdateTime(LocalDateTime.now());

            //储存修改记录日志
            InviteConfigModel oldInfo =  inviteConfigService.getById(configModel.getId());
            InviteConfigLogModel logInfo = new InviteConfigLogModel();
            logInfo.setUserId(Integer.parseInt(userId));
            logInfo.setInviteConfigId(configModel.getId());
            // 将InviteConfigModel对象转换为JSON字符串
            // PrettyFormat用于格式化输出，使其更易于阅读
            String oldJsonStr = JSON.toJSONString(oldInfo, SerializerFeature.PrettyFormat);
            logInfo.setOldJsonstr(oldJsonStr);
            String newJsonStr = JSON.toJSONString(configModel, SerializerFeature.PrettyFormat);
            logInfo.setNewJsonstr(newJsonStr);
            logInfo.setCreateTime(LocalDateTime.now());
            inviteConfigLogService.save(logInfo);

            return Result.success(this.inviteConfigService.updateById(configModel));
        } else {
            return Result.success(this.inviteConfigService.save(configModel));
        }
    }

    /**
     * 获取基本配置列表
     *
     * @return {@link Result}
     */
    @GetMapping("/admin/getBasicConfigList")
    public Result getBasicConfigList() {
        return Result.success(inviteConfigService.list());
    }

    /**
     * 按id选择基本配置
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/admin/selectBasicConfigById")
    public Result selectBasicConfigById(Integer id) {
        return Result.success(inviteConfigService.getById(id));
    }
}
